<!-- 屈光发育档案/检查信息管理(新增/修改) -->
<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center
    append-to-body fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-button class="miui-dialog-print-btn" v-if="dataForm.id" @click="print_opt(dataForm.id)">打印报告单</el-button>
    <!-- <div style="position: fixed;right: 45px;top: 75%;z-index: 9999;">
      <el-button type="primary" circle @click="getDataForm">保存</el-button>
    </div> -->
    <el-tabs v-model="activeName">
      <el-tab-pane label="三维检查" name="三维检查">
        <check-dimensional ref="oneTab" @refreshDataList="getDataList" @getStudentData="getStudentData"></check-dimensional>
      </el-tab-pane>
      <el-tab-pane label="特殊检查" name="特殊检查">
        <special-check ref="twoTab" @refreshDataList="getDataList"></special-check>
      </el-tab-pane>
      <el-tab-pane label="五大干预" name="五大干预">
        <intervene ref="threeTab" @refreshDataList="getDataList"></intervene>

      </el-tab-pane>
      <el-tab-pane label="影像采集" name="影像采集">
        <image-collect ref="fourTab" @refreshDataList="getDataList" :student-id="studentId"></image-collect>
      </el-tab-pane>
    </el-tabs>
		<span slot="footer" class="dialog-footer" v-if="formType == 'edit'">
		    <el-button size="medium" type="primary" @click="getDataForm" :loading="isCheck">保存</el-button>
		  </span>
  </el-dialog>
</template>

<script>
  import {getPvmArcOptometryObj, addPvmArcOptometryObj, putPvmArcOptometryObj} from '@/api/pvm/arcoptometry'

  import { checkFormData } from './dimensional.js'

  import CheckDimensional from './check-dimensional'
  import SpecialCheck from './arcspecialcheck-form'
  import Intervene from './intervene'
  import ImageCollect from './image-collect'
  export default {
    data(){
      return {
        visible: false,
        isCheck: false,
        formType: '',
        activeName: '',
        studentId: '',
        form1: {},
        form2: {},
        form3: {},
        form4: {},
        dataForm: {
          personInfo:{},
          optometry:{},
          specialCheck:{},
          interventionLinks: [],
          image: {}
        }
      }
    },
    components: {
      CheckDimensional,
      SpecialCheck,
      Intervene,
      ImageCollect
    },
    methods: {
      init(type,id,name,docId,customerId){
        this.visible = true
        this.formType = type
        this.activeName = name
        if(customerId){
          if(!docId){
            docId = (new Date()).valueOf()
          }
          this.$nextTick(() => {
            this.$refs.oneTab.init(type,'',customerId)
            this.$refs.twoTab.init(type)
            this.$refs.threeTab.init(type)
            this.$refs.fourTab.init(type,'',docId)
          })
        }else{
          this.dataForm.id = id || 0
          if(!docId){
            docId = (new Date()).valueOf()
          }
          this.$nextTick(() => {
            this.$refs.oneTab.init(type,this.dataForm.id,'')
            this.$refs.twoTab.init(type,this.dataForm.id)
            this.$refs.threeTab.init(type,this.dataForm.id)
            this.$refs.fourTab.init(type,this.dataForm.id,docId)
          })
        }
      },
      getDataForm(){
        this.$nextTick(() => {
          let personInfo = this.$refs.oneTab.personInfo
          let form1 = this.$refs.oneTab.dataForm
          let form2 = this.$refs.twoTab.dataForm
          let form3 = this.$refs.threeTab.dataForm
          let list = this.$refs.threeTab.meddleList
          let form4 = this.$refs.fourTab.dataForm
          this.dataForm.personInfo = personInfo
					form1.optometry.docId = form4.docId
          this.dataForm.optometry = form1.optometry
          this.dataForm.specialCheck = form2
          this.dataForm.intervention = form3
          this.dataForm.interventionLinks = form3
          this.dataForm.image = form4
          let meddleList = []
          if(list.length){
            for(let i = 0; i < list.length; i++){
              for(let j = 0; j < list[i].itemList.length; j++){
                let item = list[i].itemList[j]
                item.type = list[i].type
                if(list[i].type == '4'){
                  item.mainBallOd = list[i].mainBallOd
                  item.mainPostOd = list[i].mainPostOd
                  item.mainAxesOd = list[i].mainAxesOd
                  item.mainCvaOd = list[i].mainCvaOd
                  item.mainBallOs = list[i].mainBallOs
                  item.mainPostOs = list[i].mainPostOs
                  item.mainAxesOs = list[i].mainAxesOs
                  item.mainCvaOs = list[i].mainCvaOs
                  item.radio = list[i].radio
                }
                if(!item.materialId || !item.number || !item.materialMoney){
                  this.$message.warning('请将干预材料数量价格补充完整')
                  return false
                }
                meddleList.push(item)
              }
            }
            this.dataForm.intervention.links = meddleList
          }

          // this.dataForm.interventionLinks = meddleList
          this.dataFormSubmit();
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.isCheck = true
        let result = checkFormData(this.dataForm)
        if(result.status){
          if (this.dataForm.id) {
            putPvmArcOptometryObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
              this.isCheck = false
            }).catch(() => {
              this.isCheck = false
            })
          } else {
            addPvmArcOptometryObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
              this.isCheck = false
            }).catch(() => {
              this.isCheck = false
            })
          }
        }else{
          this.$message.error(result.message)
          this.isCheck = false
          return false
        }
        // this.$refs.oneTab.$refs['dataForm'].validate((valid) => {})
      },
      // 打印报告单
      print_opt(id){
        let routeUrl = this.$router.resolve({
          path: "/print",
          query: {id: id}
        });
        window.open(routeUrl.href, '_blank');
      },
      // 获取学生信息
      getStudentData(data){

      },
      getDataList(){
        this.visible = false
        this.$emit('refreshDataList')
      }
    }
  }
</script>

<style>

</style>
